<template>
	<view class="candle-columns">
		<canvas canvas-id="canvasCandle" id="canvasCandle" class="charts" disable-scroll=true @click="touchEndCandle"></canvas>
	</view>
</template>

<script>
	import uCharts from '@/components/uni-charts/uni-charts.min.js'
	import dataCandle from '@/static/h5/data.json'
	var canvaCandle = null
	var color = [
		'#0251f4',
		'#09f',
		'#fff',
		'#000',
	]
	export default {
		data() {
			return {
				cWidth: '',
				cHeight: '',
				pixelRatio: 1,
				itemCount: 20, //x轴单屏数据密度
				sliderMax: 50,
			}
		},
		mounted() {
			this.cWidth = uni.upx2px(750)
			this.cHeight = uni.upx2px(550)
			const {
				windowWidth,
				windowHeight
			} = uni.getSystemInfoSync()
			if (windowWidth > 750) {
				this.cWidth = 714
				this.cHeight = 474
				color = [
					'#fff',
					'#207fff',
					'#000',
					'#fff',
				]
			}
			this.getServerData()
		},
		methods: {
			getServerData() {
				let Candle = {
					categories: [],
					series: []
				}
				//这里我后台返回的是数组，所以用等于，如果您后台返回的是单条数据，需要push进去
				Candle.categories = dataCandle.data.Candle.categories
				Candle.series = dataCandle.data.Candle.series
				this.showCandle("canvasCandle", Candle)
				// uni.request({
				// 	url: '/static/h5/data.json',
				// 	data: {},
				// 	success: function(res) {
				// 		let Candle = {
				// 			categories: [],
				// 			series: []
				// 		}
				// 		//这里我后台返回的是数组，所以用等于，如果您后台返回的是单条数据，需要push进去
				// 		Candle.categories = res.data.data.Candle.categories
				// 		Candle.series = res.data.data.Candle.series
				// 		this.showCandle("canvasCandle", Candle)
				// 	},
				// 	fail: () => {
				// 		this.tips = "网络错误，小程序端请检查合法域名"
				// 	},
				// })
			},
			showCandle(canvasId, chartData) {
				canvaCandle = new uCharts({
					$this: this,
					canvasId: canvasId,
					type: 'candle',
					fontSize: 11,
					background: color[0],
					enableMarkLine: true,
					/***需要开启标记线***/
					categories: chartData.categories,
					series: chartData.series,
					animation: false,
					duration: 500,
					enableScroll: true, //开启图表拖拽功能
					title: {
						color: color[2]
					},
					xAxis: {
						disableGrid: true, //不绘制X轴网格线
						labelCount: 10, //X轴文案数量
						fontColor: color[2],
						itemCount: 10, //x轴单屏显示数据的数量，默认为5个
						scrollShow: false, //新增是否显示滚动条，默认false
						scrollAlign: 'right',
						scrollBackgroundColor: color[1], //X轴滚动条背景颜色,默认为 #EFEBEF
						scrollColor: color[2], //X轴滚动条颜色,默认为 #A6A6A6
					},
					yAxis: {
						data: [{
							axisLine: false,
							fontColor: color[2]
						}],
						gridType: 'solid',
						gridColor: color[2],
						splitNumber: 5,
						format: (val) => {
							return val.toFixed(0)
						}
					},
					width: this.cWidth,
					height: this.cHeight,
					dataLabel: false,
					dataPointShape: true,
					extra: {
						candle: {
							color: {
								upLine: '#f04864',
								upFill: '#f04864',
								downLine: '#2fc25b',
								downFill: '#2fc25b'
							},
							average: {
								show: false,
								color: color[2]
							}
						},
						tooltip: {
							bgColor: color[2],
							bgOpacity: 0.7,
							gridType: 'dash',
							dashLength: 5,
							gridColor: color[2],
							fontColor: color[3],
							horizentalLine: true,
							xAxisLabel: true,
							yAxisLabel: true,
							labelBgColor: '#DFE8FF',
							labelBgOpacity: 0.95,
							labelAlign: 'left',
							labelFontColor: color[3]
						}
					}
				})
			},
			touchEndCandle(e) {
				const query = uni.createSelectorQuery().in(this)
				query.select('.candle-columns').boundingClientRect(data => {
					e.changedTouches[0].clientX = e.changedTouches[0].clientX - data.left
					e.currentTarget.offsetTop = e.currentTarget.offsetTop + data.top - 30
				}).exec()
				canvaCandle.scrollEnd(e)
				//下面是toolTip事件，如果滚动后不需要显示，可不填写
				canvaCandle.showToolTip(e, {
					format: function(item, category) {
						return category + ' ' + item.name + ':' + item.data
					}
				})
			}
		}
	}
</script>

<style>
	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.charts {
		width: 750upx;
		height: 550upx;
		background-color: #0251f4;
	}

	/* #ifdef H5 */
	@media screen and (min-width: 750px) {
		.charts {
			margin-top: 30px;
			width: 714px;
			height: 474px;
			background-color: #fff;
		}
	}

	/* #endif */
</style>
